using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIDesignBackground : MonoBehaviour
{
    public float sizeScale = 0.4f;
    float maxSizeScale = 5;
    float minSizeScale = 0.4f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void OnScroll()
    {

        float axis = Input.GetAxis("Mouse ScrollWheel");
        Vector2 mousePos = Input.mousePosition;
        Vector2 vecMouse;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(), Input.mousePosition, null, out vecMouse);

        float newScale = sizeScale + axis * Time.deltaTime * (maxSizeScale - sizeScale) * 10;
        newScale = Mathf.Clamp(newScale, minSizeScale, maxSizeScale);

        var xScale = vecMouse.x / GetComponent<RectTransform>().sizeDelta.x;
        var yScale = vecMouse.y / GetComponent<RectTransform>().sizeDelta.x;
        var size = GetComponent<RectTransform>().sizeDelta;
        transform.position += (new Vector3(xScale * size.x, yScale* size.y, 0) * (sizeScale - newScale) );

        sizeScale = newScale;
        transform.localScale = new Vector3(sizeScale, sizeScale, 1);


    }
    bool m_IsRightDown = false;
    Vector2 m_PressPosition;
    Vector2 m_ImageBeforePressPosition;
    public void OnPressDown()
    {

        if(Input.GetMouseButtonDown(1))
        {

            if (m_IsRightDown == false)
            {
                m_PressPosition = Input.mousePosition;
                m_ImageBeforePressPosition = GetComponent<RectTransform>().position;
            }
            m_IsRightDown = true;
        }
    }
    public void OnPressUp()
    {

        m_IsRightDown = false;
    }
    public void OnPress()
    {
        if (m_IsRightDown == true)
        {
            Vector2 mousePosition = Input.mousePosition;
            GetComponent<RectTransform>().position = m_ImageBeforePressPosition + (mousePosition - m_PressPosition);
        }

    }
}
